<?php
/**
* 
*/
class ArticleModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_article';
	}

	//获取列表数据
	public function get_list_data($term='',$page=1,$size=50)
	{
		//查询搜索
		$where = ' where 1 ';
		if (isset($term['type_id']) && !empty($term['type_id'])) {

			$where .= " AND a.type_id =".$term['type_id'];
		}

		if (isset($term['keyword']) && !empty($term['keyword'])) {

			$where .= " AND a.title LIKE '%".$term['keyword']."%'";
		}

		$query = $this->db->query("SELECT a.id, a.title, a.likes, a.collection, a.share, t.name as type_name FROM ".$this->table." AS a LEFT JOIN nmp_article_type AS t ON a.type_id = t.id".$where." ORDER BY a.id DESC LIMIT ".($page-1)*$size.",".$size);

		$data = $query->result_array();

		$query = $this->db->query("select COUNT(*)  as num from ".$this->table." as a".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);
	}


	//获取单行数据
	public function get_article_row($article_id)
	{
		$this->db->select('*');
		$this->db->from($this->table);
		$this->db->where('id',$article_id);
		$query = $this->db->get();
		$data = $query->row_array();

		return $data;

	}

	//获取文章类型
	public function get_article_type()
	{
		$query = $this->db->query("SELECT id, name FROM nmp_article_type");

		return $query->result_array();

	}



	//添加入库
	public function insert_article_data($arr=array())
	{
		if ($this->db->insert($this->table,$arr)) 
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	//更新数据
	public function updata_article_data($arr =array(),$article_id ='')
	{
		if ($this->db->update($this->table,$arr,array('id'=>$article_id))){			
			return true;
		}else{
			return false;
		}

	}



	//删除
	public function remove_article($article_id)
	{
		return $this->db->delete($this->table, array('id'=>$article_id));
	}


	//添加文章类型
	public function insert_article_type($type_name='',$type_id='')
	{
		$arr['name'] = $type_name;

		if (empty($type_id)) 
		{
			if ($this->db->insert('nmp_article_type',$arr)) 
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if ($this->db->update('nmp_article_type',$arr,array('id'=>$type_id))){			
				return true;
			}else{
				return false;
			}

		}
	}
	

	//删除类型
	public function remove_type($type_id)
	{
		return $this->db->delete('nmp_article_type', array('id'=>$type_id));
	}



}





?>